package Bill.app.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

public class MessagerieEcoute extends Thread
{

    MessagerieEcoute(BufferedReader messagerieLecture, String source, String Etat, Hashtable Discussion)
    {
        this.messagerieLecture = null;
        this.source = null;
        this.Etat = null;
        this.Discussion = null;
        this.source = source;
        this.messagerieLecture = messagerieLecture;
        this.Etat = Etat;
        this.Discussion = Discussion;
        start();
    }

    public void run()
    {
        do
        {
            if(Etat.compareToIgnoreCase("CLOSED") == 0)
                stop();
            try
            {
                String mess = messagerieLecture.readLine();
                if(mess.compareToIgnoreCase("END") == 0)
                {
                    JOptionPane.showMessageDialog(null, "le serveur est inaccessible ou a reinitialis\351!!");
                    System.exit(0);
                }
                if(!mess.trim().endsWith("800") && !mess.trim().endsWith("900"))
                {
                    StringTokenizer t = new StringTokenizer(mess, ":");
                    String dest = t.nextToken().trim();
                    mess = t.nextToken();
                    Chat chat = new Chat(source, dest, mess, Etat, Discussion);
                    Discussion.put(dest, chat);
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        } while(true);
    }

    BufferedReader messagerieLecture;
    String source;
    String Etat;
    Hashtable Discussion;
}
